package com.yeah.sinaspider.utils;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * @FileName PhantomJSUtil.java
 * @Description TODO
 * @Author yeah、一页
 * @Email 1324459373@qq.com|quhailong1995@gmail.com
 * @Date 2020/4/18 17:36
 * @Version 1.0
 */
public class DriverUtil {

    @Deprecated
    private static PhantomJSDriver phantomJSDriver;
    @Deprecated
    private static ChromeDriver chromeDriver;

    //驱动池
    private static Map<String, PhantomJSDriver> phantomJSDriverMap = new HashMap<>();
    private static Map<String, ChromeDriver> chromeDriverMap = new HashMap<>();

    @Deprecated
    public static PhantomJSDriver getPhantomJSDriver() throws Exception {
        if(phantomJSDriver==null){
            PhantomJSDriver driver1 = initPhantomJSDriver();
            phantomJSDriver = driver1;
            return driver1;
        }
        return phantomJSDriver;
    }

    @Deprecated
    public static ChromeDriver getChromeDriver() throws Exception {
        if(chromeDriver==null){
            ChromeDriver driver1 = initChromeDriver();
            chromeDriver = driver1;
            return driver1;
        }
        return chromeDriver;
    }

    public static PhantomJSDriver getPhantomJSDriverFromDriverMapByKey(String key) throws Exception {
        if(phantomJSDriverMap.get(key)==null){
            PhantomJSDriver driver1 = initPhantomJSDriver();
            phantomJSDriverMap.put(key,driver1);
            return driver1;
        }
        return phantomJSDriverMap.get(key);
    }

    public static ChromeDriver getChromeDriverFromDriverMapByKey(String key) throws Exception {
        if(chromeDriverMap.get(key)==null){
            ChromeDriver driver1 = initChromeDriver();
            chromeDriverMap.put(key,driver1);
            return driver1;
        }
        return chromeDriverMap.get(key);
    }

    public static void removePhantomJSDriver(String key){
        phantomJSDriverMap.remove(key);
    }

    public static void removeChromeDriver(String key){
        chromeDriverMap.remove(key);
    }

    public static PhantomJSDriver initPhantomJSDriver() throws Exception {
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        //ssl证书支持
        desiredCapabilities.setCapability("acceptSslCerts", true);
        //截屏支持，这里不需要
        desiredCapabilities.setCapability("takesScreenshot", false);
        //css搜索支持
        desiredCapabilities.setCapability("cssSelectorsEnabled", true);


        desiredCapabilities.setCapability("--load-images=no", false);
        //js支持
        desiredCapabilities.setJavascriptEnabled(true);

        //驱动支持
        desiredCapabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,SpringContextUtil.getBean(DriverProperties.class).getPhantomPath());
        //创建无界面浏览器对象
        PhantomJSDriver driver = new PhantomJSDriver(desiredCapabilities);
        //这里注意，把窗口的大小调整为最大，如果不设置可能会出现元素不可用的问题
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        return driver;
    }

    private static ChromeDriver initChromeDriver() throws Exception {
        System.setProperty("webdriver.chrome.driver", SpringContextUtil.getBean(DriverProperties.class).getChromePath());

        ChromeOptions chromeOptions=new ChromeOptions();
        chromeOptions.addArguments("--no-sandbox");
        chromeOptions.addArguments("--disable-dev-shm-usage");
        //chromeOptions.addArguments("--headless");
        chromeOptions.addArguments("blink-settings=imagesEnabled=false");
        //chromeOptions.addArguments("--disable-gpu");

        ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
        chromeDriver.manage().window().maximize();
        chromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        return chromeDriver;
    }


}
